import { Type } from '@nestjs/common';
import { Field, Float, InputType, Int, ObjectType } from '@nestjs/graphql';
import { Prop, Schema } from '@nestjs/mongoose';
import { GraphQLBigInt } from 'graphql-scalars';
import { MongoQueryScalar } from '../scalars/mongo.query.scalar';

/** 基础通用Schema */
@ObjectType()
@Schema()
export class CommonSchema {
  @Field(() => GraphQLBigInt)
  @Prop()
  _id: number;

  @Field(() => Boolean, { defaultValue: false })
  @Prop()
  isDel: boolean;

  @Field(() => Float, { defaultValue: Date.now() })
  @Prop()
  createTime: number;

  @Field(() => Int, { defaultValue: 99999 })
  @Prop()
  sortCode: number;
}

/** 基础通用Schema */
@InputType()
export class BaseCreateSchema {
  @Field(() => Boolean, { defaultValue: false }) isDel: boolean;
  @Field(() => Float, { defaultValue: Date.now() }) createTime: number;
  @Field(() => Int, { nullable: true, defaultValue: 99999 }) sortCode: number;
}


/** 键值对列表, 用于表示字典 */
@ObjectType()
export class KeyValueList {
  @Field(() => Int)
  key: number;

  @Field(() => [GraphQLBigInt])
  value: bigint[];
}

/** 键值对列表, 用于表示字典 */
@ObjectType()
export class KeyValue {
  @Field(() => Int)
  key: number;

  @Field(() => GraphQLBigInt)
  value: bigint;
}

/** 分页返回结果 */
export function PaginatedResponse<T>(classRef: Type<T>): any {
  @ObjectType({ isAbstract: true })
  class PaginatedResponseClass {
    @Field(() => [classRef], { nullable: true })
    items: typeof classRef[];

    @Field(() => Int, { defaultValue: 0 })
    total: number;

    @Field(() => Int, { defaultValue: 0 })
    page: number;

    @Field(() => Int, { defaultValue: 10 })
    limit: number;
  }
  return PaginatedResponseClass;
}

/** 搜索输入参数 */
export function SearchInput<T>(classRef: Type<T>): any {
  @InputType({ isAbstract: true })
  class PageInputClass {
    @Field(() => classRef, { nullable: true })
    query: typeof classRef;

    @Field(() => MongoQueryScalar, { nullable: true })
    sorter: object;
  }
  return PageInputClass;
}

/** 分页输入参数 */
export function PageInput<T>(classRef: Type<T>): any {
  @InputType({ isAbstract: true })
  class PageInputClass {
    @Field(() => Int, { defaultValue: 1 })
    page: number;

    @Field(() => Int, { defaultValue: 10 })
    limit: number;

    @Field(() => classRef, { nullable: true })
    query: typeof classRef;

    @Field(() => MongoQueryScalar, { nullable: true })
    sorter: object;
  }
  return PageInputClass;
}

